package com.cstz.hb.manage.controller.applet;

import com.cstz.hb.manage.domain.AppUser;
import com.cstz.hb.manage.domain.SysQuestion;
import com.cstz.hb.manage.service.AppUserService;
import com.cstz.ydshop.common.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 小程序登录
 */
@RestController
@RequestMapping("/applet/user")
@Slf4j
public class AppletUserApiController {

	@Autowired
	private AppUserService appUserService;

    /**
     * 用户登录绑定操作
     * @param appUserDO
     * @return
     */
	@PostMapping("/login")
	public R login(@RequestBody AppUser appUserDO){
		if(StringUtils.isBlank(appUserDO.getMobile()) || StringUtils.isBlank(appUserDO.getPassword())){
			return R.error("用户名或者手机号码不能为空");
		}
		try{
            AppUser user = appUserService.getByMobile(appUserDO.getMobile());
			if(user==null){
				return R.error("用户不存在");
			}
			String encodePassword = MD5Utils.encrypt(appUserDO.getMobile(),appUserDO.getPassword());
			if(!encodePassword.equals(user.getPassword())){
				return R.error("密码错误");
			}
            user.setPassword("");
			return R.ok().put("data", user);
		}catch(Exception e){
			log.error("用户登录失败",e);
			return R.error("登录失败");
		}
	}

}
